package com.example.ivideo_2211a.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.example.common.room.VideoFavorite
import com.example.ivideo_2211a.databinding.ItemCollectionVideoBinding
import java.text.SimpleDateFormat
import java.util.*

class CollectionAdapter(
    private val onItemClick: (VideoFavorite) -> Unit,
    private val onItemLongClick: (VideoFavorite) -> Unit
) : ListAdapter<VideoFavorite, CollectionAdapter.ViewHolder>(DiffCallback()) {

    private var isSelectionMode = false
    private var selectedItems = setOf<Int>() // 确保类型为Int

    fun setSelectionMode(enabled: Boolean) {
        isSelectionMode = enabled
        notifyDataSetChanged()
    }

    fun updateSelection(selectedIds: Set<Int>) {
        selectedItems = selectedIds
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ItemCollectionVideoBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(getItem(position))
    }

    inner class ViewHolder(private val binding: ItemCollectionVideoBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bind(video: VideoFavorite) {
            // 设置视频信息
            binding.videoTitle.text = video.title
            binding.authorName.text = video.name // 使用正确的控件ID
            
            // 设置收藏时间
            val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault())
            val favoriteDate = Date(video.favoriteTime)
            binding.favoriteTime.text = "${dateFormat.format(favoriteDate)} 收藏"

            // 设置选择状态
            binding.selectionCheckbox.isChecked = selectedItems.contains(video.id) // 使用正确的控件ID
            binding.selectionCheckbox.visibility = if (isSelectionMode) android.view.View.VISIBLE else android.view.View.GONE

            // 使用Glide加载视频封面图片
            Glide.with(binding.videoThumbnail.context)
                .load(video.avatar_url)
                .apply(
                    RequestOptions()
                        .placeholder(android.R.drawable.ic_media_play)
                        .error(android.R.drawable.ic_media_play)
                        .transform(RoundedCorners(16))
                )
                .into(binding.videoThumbnail)

            binding.root.setOnClickListener {
                onItemClick(video)
            }

            binding.root.setOnLongClickListener {
                if (!isSelectionMode) {
                    onItemLongClick(video)
                }
                true
            }
        }
    }

    fun getSelectedItems(): Set<Int> = selectedItems

    fun selectAll() {
        selectedItems = currentList.map { it.id }.toSet()
        notifyDataSetChanged()
    }

    fun clearSelection() {
        selectedItems = emptySet()
        notifyDataSetChanged()
    }

    class DiffCallback : DiffUtil.ItemCallback<VideoFavorite>() {
        override fun areItemsTheSame(oldItem: VideoFavorite, newItem: VideoFavorite): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: VideoFavorite, newItem: VideoFavorite): Boolean {
            return oldItem == newItem
        }
    }
}